package demo4.util;



import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
@PropertySource("classpath:config.properties")
public class DBUtil implements IDBUtil{
    private String url;
    private String username;
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    public void setDriverClass(String className){
        System.out.println("注册驱动类");
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Value("${spring.datasource.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    @Value("${spring.datasource.username}")
    public void setUsername(String username) {
        this.username = username;
    }

    @Value("${spring.datasource.password}")
    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getConnection() throws SQLException {
//        System.out.println("DBUtil:获取数据库连接...");
        return DriverManager.getConnection(url,username,password);
    }
}
